# coding: utf-8
# 方块类
#

# build-in
import random

# pyglet
import pyglet

# 引入常数
import CONTANTS

# 引入cocos相关包
from cocos.sprite import Sprite


#
# 游戏模型
# pyglet.event.EventDispatcher 事件分发者
#

class GameModel(pyglet.event.EventDispatcher):

    def __init__(self):
        super(GameModel, self).__init__()
        # 初始化地图
        # 是一个二维数据,值为0/1
        self.map = self.init_map()
        # 初始化当前方块对象
        self.current_block = self.init_block()

    # 初始化地图
    def init_map(self):
        map = {}
        for i in range(CONTANTS.columns):
            for j in range(CONTANTS.rows):
                map[(i,j)] = 0
        return map

    # 初始化方块对象
    def init_block(self):
        # 随机选择方块形状
        return random.choice(CONTANTS.shapes)






# 方块类
class Block(Sprite):
    # img: 方块图像文件名
    # position: 方块初始位置
    # shape: 方块形状
    def __init__(self, img, position):

        super(Block, self).__init__(image=img, position=position, anchor=(0, 0))

    #TODO 下降功能
    def drop(self):
        print('droping...')

    #TODO 旋转功能
    def rotate(self):
        print('旋转...')

    def draw(self):

        pass









if __name__ == "__main__":
    from cocos.director import director
    from cocos.scene import Scene
    from cocos.layer import Layer
    img = 'data/block_blue.png'

    director.init(width=CONTANTS.win_size[0], height=CONTANTS.win_size[1], caption="Block")

    shape = random.choice(CONTANTS.shapes)
    # 存放方块的层
    layer = Layer()
    # 根据方块形状创建精灵
    for i in range(4):
        for j in range(4):
            if shape[i][j]:
                # 创建
                x = j*29
                y = (4-i)*29
                block = Block(img, (x,y))
                layer.add(block)






    sence = Scene(layer)

    director.run(sence)

















